@using ThridGroup.ERP.DTO
@model WorkshopDto
@{
    ViewData["Title"] = Model.Id > 0 ? "编辑车间" : "添加车间";
    Layout = null;
}

<link href="~/libs/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
<div class="layui-form" lay-filter="workshopForm" style="padding: 20px;">
    <input type="hidden" name="Id" value="@Model.Id">
    <input type="hidden" name="ParentId" value="@(Model.Id > 0 ? Model.ParentId : (ViewBag.ParentId ?? 0))">
    <input type="hidden" name="IsDeleted" value="false">
    
    <div class="layui-form-item">
        <label class="layui-form-label required">车间编码</label>
        <div class="layui-input-block">
            <input type="text" name="Code" value="@Model.Code" lay-verify="required" lay-reqtext="车间编码不能为空" placeholder="请输入车间编码" class="layui-input">
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label required">车间名称</label>
        <div class="layui-input-block">
            <input type="text" name="Name" value="@Model.Name" lay-verify="required" lay-reqtext="车间名称不能为空" placeholder="请输入车间名称" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">车间规模</label>
        <div class="layui-input-block">
            <input type="text" name="WorkshopSize" value="@Model.WorkshopSize" placeholder="请输入车间规模" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">负责人</label>
        <div class="layui-input-block">
            <input type="text" name="WorkshopPerson" value="@Model.WorkshopPerson" placeholder="请输入负责人" class="layui-input">
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block">
            <textarea name="Description" placeholder="请输入描述" class="layui-textarea">@Model.Description</textarea>
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label required">排序</label>
        <div class="layui-input-block">
            <input type="number" name="Index" value="@Model.Index" lay-verify="required|number" lay-reqtext="排序不能为空" placeholder="请输入排序" class="layui-input">
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label">是否启用</label>
        <div class="layui-input-block">
            <input type="checkbox" name="IsEnable" lay-skin="switch" lay-text="是|否" @(Model.IsEnable ? "checked" : "")>
        </div>
    </div>
    
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit lay-filter="submitForm" id="LAY-workshop-submit">确定</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</div>
<script src="~/libs/scripts/layui-v2.5.6/layui/layui.js"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            $ = layui.$;
        
        form.render();
        
        // 表单提交
        form.on('submit(submitForm)', function (data) {
            var field = data.field;
            var url = '/ProductionSystem/Workshop/' + (@Model.Id > 0 ? 'Update' : 'Add');
            
            // 处理复选框值
            field.IsEnable = field.IsEnable === 'on' || field.IsEnable === true || field.IsEnable === 'true';
            
            $.ajax({
                url: url,
                type: 'POST',
                contentType: 'application/json',
                data: JSON.stringify(field),
                success: function (res) {
                    if (res.code === 0) {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                        parent.layer.msg('操作成功');
                        parent.layui.table.reload('workshopTable');
                    } else {
                        layer.msg(res.msg);
                    }
                },
                error: function () {
                    layer.msg('操作失败');
                }
            });
            
            return false;
        });
    });
</script> 